package algorithm.common;

import java.util.Scanner;

/**
 * @Author: Hevery
 * @Description:
 * @Date: Created on 2017/9/9
 * @Modified By:
 */
public class Main4 {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String s1 = scan.nextLine();
        int k = scan.nextInt();
        String s2 = s1.replaceAll(" ", "");
        char[] chs = s2.toCharArray();

        for (int i=1; i<chs.length; i++) {
            if (chs[i-1] > chs[i]) {
                int j = i - 1;
                char ch = chs[i];
                while (j>-1 && ch<chs[j]) {
                    chs[j+1] = chs[j];
                    j --;
                }
                chs[j+1] = ch;
            }
        }
        for (int i=0; i<k; i++) {
            System.out.print(chs[i] + " ");
        }
        System.out.println();
        for (int i=0; i<k; i++) {
            System.out.print(chs[chs.length-1-i] + " ");
        }
    }
}
